package com.dcloud.manage.dblib.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dcloud.manage.dblib.entity.request.DbCateNodeReq;
import com.dcloud.manage.dblib.entity.vo.DatalibCategoryNodeVO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import com.dcloud.manage.dblib.entity.DatalibCategory;
import com.dcloud.manage.dblib.service.IDatalibCategoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: datalib_category
 * @Author: jeecg-boot
 * @Date:   2024-03-11
 * @Version: V1.0
 */
@Api(tags="datalib_category")
@RestController
@RequestMapping("/api/dblibcategory/v1")
@Slf4j
public class DatalibCategoryController extends JeecgController<DatalibCategory, IDatalibCategoryService> {
	@Autowired
	private IDatalibCategoryService datalibCategoryService;
	
	/**
	 * 分页列表查询
	 *
	 * @param datalibCategory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "datalib_category-分页列表查询")
	@ApiOperation(value="datalib_category-分页列表查询", notes="datalib_category-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(DatalibCategory datalibCategory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DatalibCategory> queryWrapper = QueryGenerator.initQueryWrapper(datalibCategory, req.getParameterMap());
		Page<DatalibCategory> page = new Page<DatalibCategory>(pageNo, pageSize);
		IPage<DatalibCategory> pageList = datalibCategoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param dbCateNodeReq
	  */
	 @AutoLog(value = "数据资源分类树形展示")
	 @ApiOperation(value="数据资源分类树形展示", notes="数据资源分类树形展示")
	 @PostMapping(value = "/tree")
	 public Result<?> tree(@RequestBody DbCateNodeReq dbCateNodeReq) {
		 List<DatalibCategoryNodeVO> datalibCategoryNodeVOS = datalibCategoryService.tree(dbCateNodeReq);
		 return Result.OK(datalibCategoryNodeVOS);
	 }


	
	/**
	 *   添加
	 *
	 * @param datalibCategory
	 * @return
	 */
	@AutoLog(value = "datalib_category-添加")
	@ApiOperation(value="datalib_category-添加", notes="datalib_category-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DatalibCategory datalibCategory) {
		datalibCategoryService.saveCategory(datalibCategory);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param datalibCategory
	 * @return
	 */
	@AutoLog(value = "datalib_category-编辑")
	@ApiOperation(value="datalib_category-编辑", notes="datalib_category-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DatalibCategory datalibCategory) {
		datalibCategoryService.updateById(datalibCategory);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "datalib_category-通过id删除")
	@ApiOperation(value="datalib_category-通过id删除", notes="datalib_category-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			datalibCategoryService.remove(id);
		} catch (Exception e) {
			return Result.error("删除失败！"+e.getMessage());
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "datalib_category-通过id查询")
	@ApiOperation(value="datalib_category-通过id查询", notes="datalib_category-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DatalibCategory datalibCategory = datalibCategoryService.getById(id);
		if(datalibCategory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(datalibCategory);
	}


}
